#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=350;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL dis[maxn][maxn];
int main(void){
   LL n;n=read();
   for(LL i=1;i<=n;i++){
       for(LL j=1;j<=n;j++){
           dis[i][j]=read();
       }
   }
   LL k;cin>>k;
   while(k--){
     LL a,b,c;a=read();b=read();c=read();
     for(LL i=1;i<=n;i++){
         for(LL j=1;j<=n;j++){
             dis[i][j]=min(dis[i][j],dis[i][a]+c+dis[b][j]);
             dis[i][j]=min(dis[i][j],dis[i][b]+c+dis[a][j]);
         }
     }
     LL sum=0;
     for(LL i=1;i<=n;i++){
         for(LL j=i+1;j<=n;j++){
            sum+=dis[i][j];
         }
     }
     dis[a][b]=min(dis[a][b],c);
     cout<<sum<<" ";
   }
   cout<<"\n";
   return 0;
}
